package com.my.zl.console.service.autocode.impl;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.dao.autocode.ZlKeywordDtlMapper;
import com.my.zl.console.dto.autocode.keywordDtl.ReqKeywordDtlAddDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ReqKeywordDtlDelDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ReqKeywordDtlDetailDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ReqKeywordDtlEditDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ReqKeywordDtlQueryDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ReqKeywordDtlStateDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ResKeywordDtlAddDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ResKeywordDtlDelDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ResKeywordDtlDetailDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ResKeywordDtlEditDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ResKeywordDtlQueryDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ResKeywordDtlStateDTO;
import com.my.zl.console.dto.autocode.keywordDtl.ResKeywordDtlVO2;
import com.my.zl.console.entity.autocode.ZlKeywordDtl;
import com.my.zl.console.po.autocode.keywordDtl.ZlKeywordDtlVo;
import com.my.zl.console.service.autocode.IKeywordDtlService;
import com.my.zl.console.service.base.BaseManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 数据库关键字明细表
 * @author ZL
 */
@Service
public class KeywordDtlServiceImpl extends BaseManager implements IKeywordDtlService {

    @Autowired
    private ZlKeywordDtlMapper keywordDtlMapper;

    @Override
    public ResKeywordDtlQueryDTO queryKeywordDtlList(ReqKeywordDtlQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResKeywordDtlQueryDTO resDTO = BeanUtil.copyProperties(page, ResKeywordDtlQueryDTO.class);
        if(StringUtils.isNotBlank(reqInfo.getKeywordVal())){
            reqInfo.setKeywordVal("%"+reqInfo.getKeywordVal()+"%");
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlKeywordDtlVo> list = keywordDtlMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResKeywordDtlVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResKeywordDtlDetailDTO queryKeywordDtlDetail(ReqKeywordDtlDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResKeywordDtlDetailDTO res = new ResKeywordDtlDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlKeywordDtl view = keywordDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResKeywordDtlAddDTO insertKeywordDtlInfo(ReqDTO<ReqKeywordDtlAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordDtlAddDTO reqData = reqInfo.getReqData();
        ResKeywordDtlAddDTO res = new ResKeywordDtlAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlKeywordDtl record = new ZlKeywordDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setKeywordDtlId(IdUtil.fastSimpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywordDtlId",record.getKeywordDtlId());
        //检查主键是否冲突
        if(keywordDtlMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        keywordDtlMapper.insertSelective(record);
        
        ZlKeywordDtl keyInfo = keywordDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResKeywordDtlEditDTO updateKeywordDtlInfo(ReqDTO<ReqKeywordDtlEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordDtlEditDTO reqData = reqInfo.getReqData();
        ResKeywordDtlEditDTO res = new ResKeywordDtlEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlKeywordDtl record = new ZlKeywordDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        keywordDtlMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywordDtlId",record.getKeywordDtlId());
        ZlKeywordDtl keyInfo = keywordDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResKeywordDtlDelDTO deleteKeywordDtlInfo(ReqDTO<ReqKeywordDtlDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordDtlDelDTO reqData = reqInfo.getReqData();
        ResKeywordDtlDelDTO res = new ResKeywordDtlDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        keywordDtlMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResKeywordDtlStateDTO updateKeywordDtlState(ReqDTO<ReqKeywordDtlStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordDtlStateDTO reqData = reqInfo.getReqData();
        ResKeywordDtlStateDTO res = new ResKeywordDtlStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlKeywordDtl record = new ZlKeywordDtl();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        keywordDtlMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywordDtlId",record.getKeywordDtlId());
        ZlKeywordDtl keyInfo = keywordDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}